package cn.wolfcode.web.controller;


import cn.wolfcode.domain.AnnoEmployee;
import cn.wolfcode.domain.Announcement;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.AnnouncementQueryObject;
import cn.wolfcode.service.IAnnoEmployeeService;
import cn.wolfcode.service.IAnnoLevelService;
import cn.wolfcode.service.IAnnouncementService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.PermissionAnnotation;
import cn.wolfcode.web.util.SessionUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    private IAnnouncementService announcementService;

    @Autowired
    private IAnnoLevelService levelService;

    @Autowired
    private IAnnoEmployeeService employeeService;

    @Autowired
    private IEmployeeService service;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") AnnouncementQueryObject qo){
        qo.setEmployeeId(SessionUtil.getAtt(SessionUtil.USER).getId());
        PageInfo<Announcement> pageInfo = announcementService.query(qo);
        model.addAttribute("level",levelService.selectAll());
        model.addAttribute("PageInfo", pageInfo);
        return "announcement/list";
    }


    @PermissionAnnotation(name="公告表删除",expression="announcement:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           announcementService.delete(id);
        }
        return "redirect:/announcement/list";
    }


    @PermissionAnnotation(name="公告表新增/编辑",expression="announcement:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Announcement announcement) {
        if (announcement.getId() == null) {
            announcement.setPostStatus(false);
            announcement.setCreate(SessionUtil.getAtt(SessionUtil.USER));
            announcement.setCreateTime(new Date());
            announcementService.save(announcement);
            AnnoEmployee annoEmployee = new AnnoEmployee();
            annoEmployee.setEmployeeId(SessionUtil.getAtt(SessionUtil.USER).getId());
            annoEmployee.setAnnoId(announcement.getId());
            annoEmployee.setHasRead(false);
            employeeService.insert(annoEmployee);
        } else {
            announcementService.update(announcement);
        }
        return "redirect:/announcement/list";
    }

    @RequestMapping("/putOut")
    @ResponseBody
    public String putOut(Long id){
        List<Employee> employees = service.selectAll();
        announcementService.changeIStatus(id,true);
        for (Employee employee : employees) {
            if (!employee.getAdmin()){
                AnnoEmployee annoEmployee = new AnnoEmployee();
                annoEmployee.setEmployeeId(employee.getId());
                annoEmployee.setAnnoId(id);
                annoEmployee.setHasRead(false);
                employeeService.insert(annoEmployee);
            }
        }
        return "/announcement/list";
    }

    @RequestMapping("/cancel")
    @ResponseBody
    public String cancel(Long id){
        announcementService.changeIStatus(id,false);
        List<Employee> employees = service.selectAll();
        for (Employee employee : employees) {
            if (!employee.getAdmin()){
                employeeService.delete(employee.getId(),id);
            }
        }
        return "/announcement/list";
    }

    @RequestMapping("/input")
    @ResponseBody
    public String input(Long eId,Long aId) {
        if (!announcementService.getStatus(eId,aId)) {
           announcementService.changeStatus(eId,aId);
        }
        return "/announcement/show";
    }

    @RequestMapping("/show")
    public String show(Long aId,Model model){
        model.addAttribute("announcement",announcementService.getOneById(aId));
        return "announcement/show";
    }
}
